package com.bw.park.view

import android.widget.SearchView
import android.widget.TextView
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.paging.filter
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.ItemTouchHelper.LEFT
import androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.entity.ParkBeanItem
import com.bw.lib_base.mvvm.BaseActivity
import com.bw.park.ParkAdapter
import com.bw.park.ParkViewModel
import com.bw.park.R
import com.bw.park.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
@AndroidEntryPoint
@Route(path = "/park/ParkActivity")
class ParkActivity : BaseActivity<ActivityParkBinding, ParkViewModel>() {
    val parkAdapter by lazy { ParkAdapter() }
    var type = "类型"
    var para = ""
    lateinit var datas: PagingData<ParkBeanItem>
    override fun initView() {
        binding.ll.setOnClickListener {
            ARouter.getInstance().build("/park/ParkAddActivity").navigation()
        }
       binding.parkRv.apply {
           adapter = parkAdapter
           layoutManager = LinearLayoutManager(this@ParkActivity)
       }

        //点击抽屉布局
        binding.parkFilter.setOnClickListener {
            binding.parkdrawer.openDrawer(GravityCompat.END)
        }

        binding.parkRgstatus.setOnCheckedChangeListener{ group, checkedId ->
            type = "状态"
            var para = ""
            when(checkedId){
                R.id.rb_statuskx -> para = "空闲"
                R.id.rb_stauszy -> para = "已占用"
                else-> para = "维修中"
            }
            binding.parkdrawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                viewModel.getParkByType(para,type).collect{
                    parkAdapter.submitData(it)
                }
            }


        }

        binding.parkRgtype.setOnCheckedChangeListener { group, checkedId ->
            type = "类型"
            var para = ""
            when(checkedId){
                R.id.rb_typea -> para = "标准车位"
                R.id.rb_typeb -> para = "VIP车位"
            }
            binding.parkdrawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                viewModel.getParkByType(para,type).collect{
                    parkAdapter.submitData(it)
                }
            }
        }

        binding.parkCarNo.setOnClickListener {
            type = "车牌号"
            binding.parkdrawer.closeDrawer(GravityCompat.END)

            lifecycleScope.launch {
                parkAdapter.submitData(datas.filter {
                    it.licensePlate == para
                })
            }
        }
        binding.parkArea.setOnClickListener {
            type = "区域"
            binding.parkdrawer.closeDrawer(GravityCompat.END)
            if (para.isNotEmpty()){
                lifecycleScope.launch {
                    viewModel.getParkByType(para, type).collect{
                        parkAdapter.submitData(it)
                    }
                }
            }

        }
        binding.parkSv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
               para = query!!
                lifecycleScope.launch {
                    if (type == "区域"){
                        viewModel.getParkByType(query!!,type).collect{
                            parkAdapter.submitData(it)
                        }
                    }else if(type == "车牌号"){
                        parkAdapter.submitData(datas.filter {
                            it.licensePlate == para
                        })
                    }
                }

                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }
        })

        parkAdapter.setOnItemClickListener(object :ParkAdapter.OnItemClickListener{
            override fun onItemClick(position: Int, item: ParkBeanItem) {
                ARouter.getInstance().build("/park/ParkEditActivity").withString("pid",item.id.toString()).navigation()
            }
        })

        parkAdapter.setOnItemLongClickListener(object :ParkAdapter.OnItemLongClickListener{
            override fun onItemLongClick(item: ParkBeanItem): Boolean {
                ARouter.getInstance().build("/park/ParkVRActivity").navigation()
                return true
            }

        })

        //滑动删除
        ItemTouchHelper(object :SimpleCallback(0, LEFT){
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val adapterPosition = viewHolder.adapterPosition
//                val layoutPosition = viewHolder.layoutPosition
//                val pid =
//                    viewHolder.itemView.findViewById<TextView>(R.id.park_id).text.toString()
//                viewModel.deletePark(pid.toInt())
                val item = parkAdapter.snapshot().items.get(adapterPosition)
                viewModel.deletePark(item.id!!)
            }
        }).attachToRecyclerView(binding.parkRv)
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.getParkByType("标准车位","类型").collect{
                datas = it
                parkAdapter.submitData(it)
            }
        }


        viewModel.carNumData.observe(this){
            lifecycleScope.launch {
                val mutableList = mutableListOf<ParkBeanItem>()
                mutableList.clear()
                mutableList.add(it)
                parkAdapter.submitData(PagingData.from(mutableList))
            }
        }

        viewModel.deleteData.observe(this){
            if (it){
                lifecycleScope.launch {
                    viewModel.getParkByType("标准车位",type).collect{
                        datas = it
                        parkAdapter.submitData(it)
                    }
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            viewModel.getParkByType("标准车位","类型").collect{
                datas = it
                parkAdapter.submitData(it)
            }
        }
    }
}